package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.dto.UserDto;
import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import com.example.demo.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService iUserService;

    @GetMapping
    public List<User> getUser() {
        return iUserService.list();
    }

    @PostMapping
    public boolean addUser(@RequestBody User user) {
        return iUserService.saveOrUpdate(user);
    }

    @DeleteMapping("/{id}")
    public boolean removeUser(@PathVariable int id) {
        return iUserService.removeById(id);
    }

    @PostMapping("/vo")
    public UserVo getUserVo(@RequestBody UserDto userDto) {
        System.out.println(userDto);
        return iUserService.getUserVo(userDto);
    }

    // lambda
    @PostMapping("/lmd")
    public User getUserByLmd(@RequestBody UserDto userDto) {
        return iUserService.getUserByLmd(userDto);
    }

    // list
    @PostMapping("/list")
    public List<User> getUsers(@RequestBody UserDto userDto) {
//        iUserService.page()
        return iUserService.getUsers(userDto);
    }

    // pager
    @PostMapping("/page")
    public IPage<User> getPage(@RequestBody UserDto userDto) {
        return iUserService.getUserByPager(userDto);
    }

    // 事务
    @PutMapping
    public int editUser(@RequestBody UserDto userDto) {
        log.info("userDto:{}", userDto);
        return iUserService.updateUser(userDto);
    }

}
